package com.crack.spansframe;

import android.util.Log;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * span集合
 * Created by liuyu on 16-4-12.
 */
public class SpanSet {
    public static final int QUOTE = 1;
    public static final int BOLD = 2;
    public static final int COLOR = 3;
    public static final int SIZE = 4;
    public static final int URL = 5;
    public static final int LIST = 6;
    public static final int IMAGE = 7;
    public static final int ALIGN = 8;
    public static final int LINE = 9;
    public static final int COLL = 10;
    public static final int UDER_LINE = 11;
    public static final int ITATIC = 12;
    public static final int DEL = 13;
    public static final int UNDER = 14;
    int[] starts;
    int[] ends;
    int[] flags;
    Object[][] args;
    int len;
    int count;

    SpanSet() {
        this(5);
    }

    SpanSet(int len) {
        this.len = len;
        starts = (int[]) Array.newInstance(int.class, len);
        ends = (int[]) Array.newInstance(int.class, len);
        flags = (int[]) Array.newInstance(int.class, len);
        args = (Object[][]) Array.newInstance(Object[].class, len);
    }

    void put(int start, int end, int flag, Object... args) {
        if (count == len) {
            len *= 2;
            starts = Arrays.copyOf(starts, len);
            ends = Arrays.copyOf(ends, len);
            flags = Arrays.copyOf(flags, len);
            this.args = Arrays.copyOf(this.args, len);
        }
        starts[count] = start;
        ends[count] = end;
        flags[count] = flag;
        if (args.length > 0) {
            this.args[count] = args;
        }
        count++;
    }

    void offset(int pos, int len) {
        delete(starts, pos, len);
        delete(ends, pos, len);
    }

    void delete(int[] src, int pos, int len) {
        if (len == 0) {
            return;
        }
        for (int i = 0; i < count; i++) {
            if (len > 0 && src[i] > pos || len < 0 && src[i] >= pos) {
                src[i] -= len;
            }
        }
    }
}
